/**
 * 
 */
package profile.report;

import groovy.lang.Writable;
import groovy.text.GStringTemplateEngine;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ductrung.tran
 *
 */
public class HtmlReportPrinter implements ResultsReportPrinter {

	public void print(ResultsReport report) {
		try {
			PrintWriter output = getOutput(report);
			print(report, output);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void print(ResultsReport report, PrintWriter output) throws Exception {
		GStringTemplateEngine engine = new GStringTemplateEngine();
		Map<String, Object> binding = new HashMap<String, Object>();
		binding.put("report", report);
		try {
			Writable template = engine.createTemplate(new File("./src/main/groovy/htmlReport.tpl.jsp")).make(binding);
			output.print(template.toString());
		} finally {
			output.close();
		}
	}

	private PrintWriter getOutput(ResultsReport report) throws IOException {
		return new PrintWriter(new FileWriter(report.getFirstName() + "-" + report.getSecondName() + ".html"));
	}

}
